package com.rrd.dw.mr.gzip.kafka;

import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.sms.*;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 解析短信信息
 *
 * @author xj
 */

public class NewSmsReportParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 解析用户短信基础数据
     *
     * @param model
     * @throws InterruptedException
     * @throws IOException
     * @return
     */
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        SmsReportRecord obj = (SmsReportRecord) model;

        SmsAppInfo appInfo = obj.getSmsAppInfo() == null ? new SmsAppInfo() : obj.getSmsAppInfo();
        SmsUserInfo userInfo = obj.getSmsUserInfo() == null ? new SmsUserInfo() : obj.getSmsUserInfo();
        SmsDeviceInfo deviceInfo = obj.getSmsDeviceInfo() == null ? new SmsDeviceInfo() : obj.getSmsDeviceInfo();
        StringBuilder sb = getBaseInfo(obj, successful);
        String timestamstr = obj.getTimestamp() == null ? "" : DateUtil.toDateStrMs(new Date(obj.getTimestamp()));

        //系统唯一KEY
        sb.append(userInfo.getSystem_unique_key() == null ? "" : userInfo.getSystem_unique_key()).append(CRT001);
        //手机号
        sb.append(userInfo.getPhone() == null ? "" : userInfo.getPhone()).append(CRT001);
        //应用唯一标识
        sb.append(appInfo.getApp_bundle() == null ? "" : appInfo.getApp_bundle()).append(CRT001);
        //应用版本
        sb.append(appInfo.getApp_version() == null ? "" : appInfo.getApp_version()).append(CRT001);
        //是否越狱
        sb.append(deviceInfo.getJailbroken() == null ? "" : deviceInfo.getJailbroken()).append(CRT001);
        //屏幕尺寸
        sb.append(deviceInfo.getScreen() == null ? "" : deviceInfo.getScreen()).append(CRT001);
        //模拟器
        sb.append(deviceInfo.getSimulator() == null ? "" : deviceInfo.getSimulator()).append(CRT001);
        //网络类型
        sb.append(deviceInfo.getNetwork_type() == null ? "" : deviceInfo.getNetwork_type()).append(CRT001);
        //操作系统
        sb.append(deviceInfo.getOs() == null ? "" : deviceInfo.getOs()).append(CRT001);
        //是否wifi
        sb.append(deviceInfo.getWifi() == null ? "" : deviceInfo.getWifi()).append(CRT001);
        //设备号
        sb.append(deviceInfo.getImei() == null ? "" : deviceInfo.getImei()).append(CRT001);
        //设备名
        sb.append(deviceInfo.getDevice_name() == null ? "" : deviceInfo.getDevice_name()).append(CRT001);
        //系统语言
        sb.append(deviceInfo.getLanguage() == null ? "" : deviceInfo.getLanguage()).append(CRT001);
        //系统版本
        sb.append(deviceInfo.getOs_version() == null ? "" : deviceInfo.getOs_version()).append(CRT001);
        //厂商
        sb.append(deviceInfo.getModel() == null ? "" : deviceInfo.getModel()).append(CRT001);
        //运营商
        sb.append(deviceInfo.getCarrier() == null ? "" : deviceInfo.getCarrier()).append(CRT001);

        StringBuilder detailSb = new StringBuilder(200);
        List<SmsContentInfo> list = obj.getSmsContentInfo();

        setFileNum(30);
        int i = 0;
        for (SmsContentInfo o : list) {
            i++;
            detailSb.delete(0, detailSb.length());
            //短信主键编号
            detailSb.append(o.get_id() == null ? "" : o.get_id()).append(CRT001);
            //会话的id
            detailSb.append(o.getThread_id() == null ? "" : o.getThread_id()).append(CRT001);
            //接收人电话
            detailSb.append(o.getAddress() == null ? "" : o.getAddress()).append(CRT001);
            //接收人姓名
            detailSb.append(o.getPerson() == null ? "" : o.getPerson()).append(CRT001);
            //发收时间
            detailSb.append(o.getDate() == null ? "" : o.getDate()).append(CRT001);
            //协议
            detailSb.append(o.getProtocol() == null ? "" : o.getProtocol()).append(CRT001);
            //短信类型
            detailSb.append(o.getType() == null ? "" : o.getType()).append(CRT001);
            //是否阅读
            detailSb.append(o.getRead() == null ? "" : o.getRead()).append(CRT001);
            //发出的短信状态
            detailSb.append(o.getStatus() == null ? "" : o.getStatus()).append(CRT001);
            //短信主题
            detailSb.append(o.getSubject() == null ? "" : o.getSubject()).append(CRT001);
            //短信内容
            detailSb.append(o.getBody() == null ? "" : o.getBody()).append(CRT001);
            //创建人
            detailSb.append(o.getCreator() == null ? "" : o.getCreator()).append(CRT001);
            detailSb.append(timestamstr).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + detailSb.toString()).replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        if (i == 0) {
            key.set(getKey());
            val.set(sb.toString().replace("\n", " ").replace("\r", " "));
            context.write(key, val);
        }

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_sms_report_inc";
    }

    @Override
    public String getType() {
        return RecordType.SMS_REPORT.toString();
    }

    @Override
    public String getComment() {
        return "短信信息";
    }


}
